package com.book.bookmanage.utils;

import com.book.bookmanage.config.execption.CustomException;
import com.book.bookmanage.constant.ResponseEnum;
import lombok.Builder;
import lombok.Data;

import java.io.Serializable;

@Data
@Builder
public class Result<T> {
    private int code;
    private T data;
    private String msg;

    public static <T> Result<T> success(T data) {
        return Result.<T>builder().data(data)
                .msg(ResponseEnum.SUCCESS.getMsg())
                .code(ResponseEnum.SUCCESS.getCode())
                .build();
    }

    public static <T> Result<T> success() {
        return success(null);
    }

    public static <T> Result<T> fail(ResponseEnum responseEnum, T data) {
        return Result.<T>builder()
                .data(data)
                .msg(responseEnum.getMsg())
                .code(responseEnum.getCode())
                .build();
    }

    public static <T> Result<T> fail(CustomException customException) {
        return Result.<T>builder()
                .msg(customException.getMsg())
                .code(customException.getCode())
                .build();
    }
}
